﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BA2009.DataAccess.Context;
using BA2009.Entities.Helpers;

namespace BA2009.Data.DataAccess
{
    public class MapTablesRepository:  IMapTablesRepository
    {
        protected BA2009Context _db;

        private string id1Name;
        private string id2Name;
        private string tableName;

        public MapTablesRepository(string id1Name, string id2Name, string tableName)
        {
            this.id1Name = id1Name;
            this.id2Name = id2Name;
            this.tableName = tableName;

            _db = new BA2009Context(BA2009.Context.Constants.CONNECTION_STRING);
        }

        #region IMapTablesRepository Members

        public bool Insert(IList<BA2009.Entities.MapEntity> entities, BA2009Context db)
        {
            bool ret = true;

            foreach (var entity in entities)
            {
                string cmd = string.Format("INSERT INTO {0} ({1},{2}) VALUES ({3},{4})", tableName, id1Name, id2Name, entity.ID1, entity.ID2);
                int affects = db.ExecuteCommand(cmd);
                if (affects == 0)
                    ret = false;
            }

            return ret;
        }

        public bool Update(IList<BA2009.Entities.MapEntity> entities, BA2009Context db)
        {

            bool ret = true;

            if (entities.Count == 0)
                throw new InvalidOperationException("List<MapEntity> trong hàm Update của MapTablesRepository là sai.");

            int id1=entities[0].ID1;
            
            // Get all resource id map
            string query = string.Format("SELECT {0} ID1, {1} ID2 FROM {2} WHERE {0} = {3}", id1Name, id2Name,tableName, id1);

            IList<Entities.MapEntity> mapEntities = db.ExecuteQuery<Entities.MapEntity>(query).ToList();
           

            // new map entities
            IList<int> newMaps = new List<int>();
            // not changed map entities
            IList<int> notChangedMaps = new List<int>();

            // deleting map entites
            IList<int> deletingMaps = new List<int>();

            foreach (var entity in entities)
            {
                if (mapEntities.ContainID2(entity.ID2))
                {
                    notChangedMaps.Add(entity.ID2);
                }
                else if (entity.ID2 > 0)
                {
                    newMaps.Add(entity.ID2);
                }

            }

            foreach (var mapEntity in mapEntities)
            {
                if (!entities.ContainID2(mapEntity.ID2))
                {
                    deletingMaps.Add(mapEntity.ID2);
                }
            }

            // Insert
            //if (!this.Insert(newMaps, db))
            //    return false;

            foreach (var newMap in newMaps)
            {
                string cmd = string.Format("INSERT INTO {0} ({1},{2}) VALUES ({3},{4})", tableName, id1Name, id2Name, id1,newMap);
                int affects = db.ExecuteCommand(cmd);
                if (affects == 0)
                    ret = false;
            }

            // Delete
            foreach (var deleting in deletingMaps)
            {
                string cmd = string.Format("DELETE {0} WHERE {1} = {2} AND {3} = {4}",this.tableName,this.id1Name,id1,this.id2Name,deleting);
                 int affected=db.ExecuteCommand(cmd);
                 if (affected == 0)
                     ret = false;
            }

            return ret;
        }

        public bool CheckExisted(BA2009.Entities.MapEntity entity)
        {
            string cmd = string.Format("SELECT COUNT(*) FROM {0} WHERE {1} = {2} AND {3} = {4}", tableName, id1Name, entity.ID1, id2Name, entity.ID2);
            int count = _db.ExecuteQuery<int>(cmd).First();

            return count > 1 ? true : false;

        }

        #endregion
    }
}
